package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPageVo;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description NursingPlanServiceImpl
 * @Author huangdongye
 * @Date 2025-05-08  17:03
 */
@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;


    @Override
    public List<NursingPlanVo> allNursingPlan() {
        return nursingPlanMapper.allNursingPlan();
    }


    @Override
    @Transactional
    public void addNursingPlan(NursingPlanVo npv) {

        npv.setCreateTime(LocalDateTime.now());
        npv.setUpdateTime(LocalDateTime.now());
        npv.setStatus(0);
        nursingPlanMapper.addNursingPlan(npv);
        Long id = npv.getId();
        System.out.println("id:" + id);
        if (!CollectionUtils.isEmpty(npv.getProjectPlans())) {
            for (NursingProjectPlanVo projectPlanVo : npv.getProjectPlans()) {
                projectPlanVo.setPlanId(id);
                projectPlanVo.setCreateTime(LocalDateTime.now());
                projectPlanVo.setUpdateTime(LocalDateTime.now());
            }
            nursingPlanMapper.addNursingProPlan(npv.getProjectPlans());
        }

    }

    @Override
    public PageResponse<NursingPlanVo> PageNursingPlan(NursingPageVo nursingPlan) {
        PageHelper.startPage(nursingPlan.getPageNum(), nursingPlan.getPageSize());
        Page<NursingPlanVo> page = nursingPlanMapper.PageNursingPlan(nursingPlan);
        return PageResponse.of(page, NursingPlanVo.class);
    }

    @Override
    public NursingPlanVo NursingPlanById(Long id) {
        return nursingPlanMapper.NursingPlanById(id);
    }

    @Override
    public void updateNursingPlanById(NursingPlanVo NursingPlan) {
        NursingPlan.setUpdateTime(LocalDateTime.now());
        nursingPlanMapper.updateNursingPlanById(NursingPlan);
        // 删除护理重新添加
        nursingPlanMapper.deleteNursingProjectPlan(NursingPlan.getId());
        // 重新新增
        if (!CollectionUtils.isEmpty(NursingPlan.getProjectPlans())) {
            for (NursingProjectPlanVo projectPlanVo : NursingPlan.getProjectPlans()) {
                projectPlanVo.setPlanId(NursingPlan.getId());
                projectPlanVo.setCreateTime(LocalDateTime.now());
                projectPlanVo.setUpdateTime(LocalDateTime.now());
            }
            nursingPlanMapper.addNursingProPlan(NursingPlan.getProjectPlans());
        }
    }

    @Override
    @Transactional
    // 根据ID删除
    // 事务回滚
    public void deleteNursingPlan(Long id) {
        // 先删除护理计划
        nursingPlanMapper.deleteNursingPlan(id);
        // 在删除护理计划项目
        nursingPlanMapper.deleteNursingProjectPlan(id);
    }

    @Override
    public void updateSwitch(Long id, Integer status) {
        nursingPlanMapper.updateSwitch(id,status);
    }
}
